package tc.de.krawallmann.gravoblocks.twl.widgets;

import java.util.ArrayList;
import java.util.List;

import tc.de.krawallmann.gravoblocks.game.model.TetrisBlock;
import tc.de.krawallmann.gravoblocks.game.model.TetrisGrid;
import de.matthiasmann.twl.BoxLayout;
import de.matthiasmann.twl.GUI;

/**
 * The Class GridView displays a TetrisGrid on the screen, using the TWL
 * library.
 * @author KrawallMann
 * @version 1.0
 */
public class GridView extends BoxLayout {

	/** The views. */
	protected transient final List<BlockView> views;

	/** The blocks x. */
	private transient final int blocksX;

	/** The blocks y. */
	private transient final int blocksY;

	/** The grid. */
	private transient final TetrisGrid grid;

	/**
	 * Instantiates a new grid view.
	 * 
	 * @param grid
	 *            the grid
	 */
	public GridView(final TetrisGrid grid) {
		super();
		this.grid = grid;
		views = new ArrayList<BlockView>();
		blocksX = grid.getWidth();
		blocksY = grid.getHeight();
	}

	/*
	 * (non-Javadoc)
	 * @see de.matthiasmann.twl.Widget#adjustSize()
	 */
	@Override
	public void adjustSize() {
		layout();
	}

	/*
	 * (non-Javadoc)
	 * @see de.matthiasmann.twl.BoxLayout#getMinHeight()
	 */
	@Override
	public int getMinHeight() {
		return (blocksY + 1) * 28;
	}

	/*
	 * (non-Javadoc)
	 * @see de.matthiasmann.twl.BoxLayout#getMinWidth()
	 */
	@Override
	public int getMinWidth() {
		return (blocksX + 1) * 28;
	}

	/**
	 * Initializes the.
	 */
	public void init() {
		init(true);
	}

	/**
	 * Initializes the.
	 * 
	 * @param blinkingGhost
	 *            the blinking ghost
	 */
	public void init(final boolean blinkingGhost) {
		int row;
		if (getTheme().contains("topopen")) {
			row = 1;
		} else {
			row = 0;
		}
		for (; row < grid.getHeight(); row++) {
			for (final TetrisBlock block : grid.getRow(row)) {
				final BlockView blockView = new BlockView(block);
				if (blinkingGhost) {
					blockView.setTheme("/blockview");
				} else {
					blockView.setTheme("/blockviewnoghost");
				}
				views.add(blockView);
				blockView.setBlocked(block.isBlocked());
				blockView.setColor(block.getColor());
				blockView.setTransparent(block.isTransparent());
				blockView.setMarkedToRemove(block.isMarkedToRemove());
				blockView.setGhost(block.isGhost());
				add(blockView);
			}
		}
	}

	public void setGameover(final boolean gameover) {
		for (int y = 0; y < blocksY; y++) {
			for (int x = 0; x < blocksX; x++) {
				// Make the blocks blink!
				if (grid.getBlockAt(x, y).isBlocked()) {
					grid.getBlockAt(x, y).setBlocked(false);
					grid.getBlockAt(x, y).setGhost(true);
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see de.matthiasmann.twl.BoxLayout#layout()
	 */
	@Override
	protected void layout() {
		super.layout();
		int row = 0;
		int col = 0;
		if (getTheme().contains("topopen")) {
			setSize((blocksX + 1) * 28, (blocksY) * 28 - 14);
		} else {
			setSize((blocksX + 1) * 28, (blocksY + 1) * 28);
		}
		for (final BlockView v : views) {
			if (col >= blocksX) {
				++row;
				col = 0;
			}
			++col;
			if (getTheme().contains("topopen")) {
				v.setPosition(getX() - 14 + col * 28, getY() + (row) * 28);
			} else {
				v.setPosition(getX() - 14 + col * 28, getY() + 14 + row * 28);
			}

			v.setSize(28, 28);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see de.matthiasmann.twl.Widget#paint(de.matthiasmann.twl.GUI)
	 */
	@Override
	protected void paint(final GUI gui) {
		paintChildren(gui);
		paintBackground(gui);
		paintWidget(gui);
		paintOverlay(gui);
	}
}
